package P4;

import org.junit.Test;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author DJ同学
 * @Date 2021/2/24 8:45
 * @Version 1.0
 * @Name 寻找两个正序数组的中位数
 * @Problem  https://leetcode-cn.com/problems/median-of-two-sorted-arrays/
 *
 */
public class Solution {
    @Test
    public void test(){
        double medianSortedArrays = findMedianSortedArrays(new int[]{1, 3}, new int[]{2});
        System.out.println(medianSortedArrays);
    }

    public double findMedianSortedArrays(int[] nums1, int[] nums2) {
        int len1 = nums1.length;
        int len2 = nums2.length;
        List<Integer> nums = new ArrayList<>();
        int i = 0;
        int j= 0;
        while(i<len1&&j<len2){
            if(nums1[i]>nums2[j]){
                nums.add(nums2[j++]);
            }else{
                nums.add(nums1[i++]);
            }
        }
        while(i<len1){
            nums.add(nums1[i++]);
        }
        while(j<len2){
            nums.add(nums2[j++]);
        }
        return (nums.get((len1 + len2 + 1)/2-1)+nums.get((len1+len2+2)/2-1))/2.0;
    }
}
